package com.deyuanyun.pic.risk.controller;

import com.deyuanyun.pic.common.dto.PageJsonBean;
import com.deyuanyun.pic.common.util.ajax.AjaxSupport;
import com.deyuanyun.pic.common.util.converter.BeanConverter;
import com.deyuanyun.pic.common.util.poi.ExcelUtil;
import com.deyuanyun.pic.common.util.web.WebUtil;
import com.deyuanyun.pic.risk.controller.dto.*;
import com.deyuanyun.pic.risk.controller.vo.HcaInfoVO;
import com.deyuanyun.pic.risk.domain.HcaInfo;
import com.deyuanyun.pic.risk.service.HcaService;
import com.deyuanyun.pic.settings.controller.AbstractBaseController;
import com.deyuanyun.pic.settings.util.DownExcelUtil;
import com.github.pagehelper.Page;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/riskMgr/hca")
public class HcaController extends AbstractBaseController {

	@Resource
	HcaService hcaService;

	/**
	 * 跳转到 高后果区管理 页面
	 *
	 * @return
     */
	@RequestMapping("/toHcaMgrList.html")
	public ModelAndView toHcaMgrList() {
		ModelAndView mav = new ModelAndView();
		mav.setViewName("/riskmgr/hcaMgrList");
		return mav;
	}

	/**
	 * 根据条件查询"高后果区"信息
	 *
	 * @return
	 */
	@RequestMapping("/queryHca.json")
	@ResponseBody
	public void queryHca(HcaInfoQuery hcaInfoQuery) {
		PageJsonBean jsonBean = new PageJsonBean();
		Page<HcaInfo> hcaInfoList = hcaService.queryHca(hcaInfoQuery);
		jsonBean.setList(new HcaInfoVO().forDomain(hcaInfoList));
		AjaxSupport.sendSuccessText("", jsonBean);
	}

	/**
	 * 跳转到 高后果区地图 页面
	 *
	 * @return
	 */
	@RequestMapping("/toHcaMap.html")
	public ModelAndView toHcaMap(String gisArray) {
		ModelAndView mav = new ModelAndView();
		mav.addObject("gisArray", gisArray);
		mav.setViewName("/riskmgr/hcaMap");
		return mav;
	}

	/**
	 * 跳转到 高后果区统计 页面
	 *
	 * @return
	 */
	@RequestMapping("/toStatistics.html")
	public ModelAndView toStatistics() {
		ModelAndView mav = new ModelAndView();
		mav.setViewName("/riskmgr/hacStatistics");
		return mav;
	}

	/**
	 * 根据条件 统计已评价的高后果区管段占比
	 *
	 * @return
	 */
	@RequestMapping("/statistics.json")
	@ResponseBody
	public void statistics(HcaStatisticsQuery hcaStatisticsQuery) {
		AjaxSupport.sendSuccessText("", hcaService.statistics(hcaStatisticsQuery));
	}

	/**
	 * 根据条件导出"高后果区"信息
	 *
	 * @return
	 */
	@RequestMapping("/export.json")
	@ResponseBody
	public void export(HcaInfoQuery hcaInfoQuery) throws IOException {

		Page<HcaInfo> hcaInfoList = hcaService.queryHca(hcaInfoQuery);
		List<HcaInfoVO> hcaInfoVOList = new HcaInfoVO().forDomain(hcaInfoList);

		String[] columnKeys = {"code", "pipeLineName", "hcaNumber", "hcaLength",
				"fromMileage", "fromReference", "fromReferenceDistance", "toMileage", "toReference", "toReferenceDistance", "fromLocationDescription", "toLocationDescription",
				"riskDate", "threatScore", "description", "hacGrade", "influenceRadius", "recrder", "recordTime", "remark"};
//		String[] columnNames = {"统一编号", "管道", "管段", "高后果区编号", "高后区长度(m)",
//				"起点绝对距离（m）", "起点最近参考", "起点相对最近参考点距离（m）", "终点绝对距离（m））", "终点最近参考", "终点相对最近参考点距离（m）",
//				"评价时间", "可能存在的威胁总分", "高后果区描述", "高后果区等级" "潜在影响半径（m）", " 记录人", "记录时间", "备注"};
		DownExcelUtil.export("高后果区数据.xls", "/template/excl/risk/hca_template.xls", 2, hcaInfoVOList, columnKeys);
	}
}
